package com.xyzboom.web.servlets.course;

import com.xyzboom.beans.CourseDeleteResult;
import com.xyzboom.database.dao.HitCourseDAO;
import com.xyzboom.database.entity.Course;
import com.xyzboom.database.entity.User;
import com.xyzboom.utils.WebUtils;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import com.xyzboom.web.services.CourseService;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@WebServlet(name = "CourseController", urlPatterns = "/servlets/course/CourseController")
public class CourseController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        WebUtils.forward("/CourseService", request, response);
        Enumeration<String> params = request.getParameterNames();
        List<Integer> ids = new ArrayList<>();
        while (params.hasMoreElements()) {
            String s = params.nextElement();
            if (request.getParameter(s).equals("on")) {
                try {
                    ids.add(Integer.parseInt(s));
                } catch (NumberFormatException ignore) {
                }
//                System.out.println("added " + s);
            }
        }
        String hitCourse = request.getParameter("hitCourse");
        if (ids.isEmpty() && hitCourse == null) {
            String id = request.getParameter("id");
            if (id == null) {
                WebUtils.redirect("/pages/course/allCourse.jsp", request, response);
                return;
            }
            String name = request.getParameter("name");
            String num = request.getParameter("num");
            String type = request.getParameter("type");
            Course course = new Course(Integer.parseInt(id), name, Integer.parseInt(num), type);
            switch (CourseService.addNewCourse(course)) {
                case SUCCESS:
                    WebUtils.redirect("/pages/course/allCourse.jsp", request, response);
                    break;
                case FAILURE:
                    toFailurePage(request, response, "由于已经存在课程或添加冲突，添加新课程失败");
                    break;
                case SQLException:
                    toFailurePage(request, response, "由于数据连接异常，添加新课程失败");
                    break;
            }
            return;
        }
        if (hitCourse == null) {
            CourseDeleteResult result = CourseService.deleteCourse(ids);
            if (result.getSuccessIds().isEmpty()) {
                toFailurePage(request, response, "数据库错误，删除操作失败！");
            } else {
                request.setAttribute("del_result", result);
                WebUtils.forward("/pages/course/delCourse.jsp", request, response);
            }
        } else {
            User user = (User) request.getSession().getAttribute("user");
//    System.out.println(user);
            if (user == null) {
                response.getWriter().println("您需要登录");
                return;
            }
            try {
                HitCourseDAO.addHitCourses(user.getId(), ids);
            } catch (SQLException e) {
                request.getSession().setAttribute("addCourseFailure", new Object());
//                e.printStackTrace();
            }
            WebUtils.redirect("/pages/course/hitCourse.jsp", request, response);
        }
    }

    private void toFailurePage(HttpServletRequest req, HttpServletResponse resp, String reason) throws ServletException, IOException {
        req.setAttribute("reason", reason);
        WebUtils.forward("/pages/course/courseFailure.jsp", req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
